package com.looookme.shaozhuo.datasave;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

/**
 * Created by Shaozhuo on 2015/9/11.
 */
public class Data_SqlLite extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
    }

    private void initView() {
        LinearLayout ll = new LinearLayout(this);
        ll.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        ll.setOrientation(LinearLayout.VERTICAL);
        setContentView(ll);

        Button saveBnt = new Button(this);
        saveBnt.setText("Save SqlLite Btn");
        saveBnt.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        saveBnt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建SqlLite 数据库
                SQLiteDatabase db = openOrCreateDatabase("myDb.db", MODE_PRIVATE, null);
                //创建数据表
                db.execSQL("create table if not exists tb_user(_id integer primary key autoincrement, name text not null, age integer not null, sex text not null)");
                //插入数据
                db.execSQL("insert into tb_user(name, age, sex) values('Verein', 23, 'male')");
                //插入数据
                ContentValues cValues = new ContentValues();
                cValues.put("name", "Breath");
                cValues.put("age", 24);
                cValues.put("sex", "男");
                db.insert("tb_user", null, cValues);
                //关闭数据连接
                db.close();

                Toast.makeText(Data_SqlLite.this, "数据插入成功", Toast.LENGTH_SHORT).show();
            }
        });
        ll.addView(saveBnt);

        Button updateBtn = new Button(this);
        updateBtn.setText("Update SqlLite Btn");
        updateBtn.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        updateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = openOrCreateDatabase("myDb.db", MODE_PRIVATE, null);
                //修改
                ContentValues cValue = new ContentValues();
                cValue.put("sex", "female");
                db.update("tb_user", cValue, "sex=?", new String[]{"男"});
                db.close();

                Toast.makeText(Data_SqlLite.this, "修改成功", Toast.LENGTH_LONG).show();
            }
        });
        ll.addView(updateBtn);

        Button delBtn = new Button(this);
        delBtn.setText("Delete SqlLite Btn");
        delBtn.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        delBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = openOrCreateDatabase("myDb.db", MODE_PRIVATE, null);
                //删除
                db.delete("tb_user", "sex like ?", new String[]{"%e%"});
                db.close();

                Toast.makeText(Data_SqlLite.this, "数据删除", Toast.LENGTH_LONG).show();
            }
        });
        ll.addView(delBtn);

        Button getBtn = new Button(this);
        getBtn.setText("Get SqlLite Btn");
        getBtn.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        getBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = openOrCreateDatabase("myDb.db", MODE_PRIVATE, null);
//                Cursor c = db.rawQuery("select * from tb_user", null);
//                if (c != null) {
//                    while (c.moveToNext()) {
//                        Log.i("SSSSSS", c.getInt(c.getColumnIndex("_id")) + " - " + c.getString(c.getColumnIndex("name")) + " - " + c.getInt(c.getColumnIndex("age")) + " - " + c.getString(c.getColumnIndex("sex")));
//                    }
//                    c.close();
//                }

                Cursor c = db.query("tb_user", new String[]{"name", "sex"}, null, null, null, null, null, "40");
                if (c != null) {
                    while (c.moveToNext()) {
                        Log.i("SSSSSS", c.getString(c.getColumnIndex("name")) + " - " +  " - " + c.getString(c.getColumnIndex("sex")));
                    }
                    c.close();
                }
                db.close();
            }
        });
        ll.addView(getBtn);
    }
}
